<%--
  Created by IntelliJ IDEA.
  User: 24258
  Date: 2022/8/30
  Time: 23:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>

<!DOCTYPE html>
<html lang="zh-CN">
<%@include file="/WEB-INF/include-head.jsp" %>
<link rel="stylesheet" href="ztree/zTreeStyle.css">
<script type="text/javascript" src="ztree/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript" src="crowd/my-menu.js"></script>
<script type="text/javascript" src="crowd/my-assign.js"></script>
<script type="text/javascript">
    $(function () {
        generateMenuTree();

        // 添加子节点绑定响应事件
        $("#treeDemo").on("click", ".addBtn", function () {
            $("#menuAddModal").modal("show");
            window.currentPid = this.id;
            return false;
        })


        // 绑定提交添加按钮
        $("#menuSaveBtn").click(function () {
            var name = $.trim($("#menuAddModal [name=name]").val());
            var url = $.trim($("#menuAddModal [name=url]").val());
            var icon = $("#menuAddModal [name=icon]:checked").val()

            $.ajax({
                url: "menu/save.json",
                type: "post",
                data: {
                    "pid": window.currentPid,
                    "name": name,
                    "url": url,
                    "icon": icon
                },
                dataType: "json",
                success: function (response) {
                    var result = response.result;
                    if (result == "SUCCESS") {

                        layer.msg("添加成功");
                        // 清理模态框
                        $("#menuResetBtn").click();
                        // 重新加载页面
                        generateTree();
                        return;
                    }

                    if (result == "FAILED") {
                        layer.msg("添加失败" + response.message);
                        return;
                    }

                },
                error: function (response) {
                    layer.msg("错误代码：" + response.status + ",错误信息：" + response.statusText);
                }
            })
            $("#menuAddModal").modal("hide");

        })

        // 修改子节点绑定响应事件
        $("#treeDemo").on("click", ".editBtn", function () {
            $("#menuEditModal").modal("show");
            var zthreeObj = $.fn.zTree.getZTreeObj("treeDemo");
            window.id = this.id;
            var key = "id";
            var value = window.id;
            var currentNode = zthreeObj.getNodeByParam(key, value);

            $("#menuEditModal [name=name]").val(currentNode.name);
            $("#menuEditModal [name=url]").val(currentNode.url);
            $("#menuEditModal [name=icon]").val([currentNode.icon]);
            return false;
        })

        $("#menuEditBtn").click(function () {

            $.ajax({
                url: "menu/edit.json",
                type: "POST",
                dataType: "json",
                data: {
                    "id":window.id,
                    "name": $("#menuEditModal [name=name]").val(),
                    "url": $("#menuEditModal [name=url]").val(),
                    "icon": $("#menuEditModal [name=icon]:checked").val()
                },
                success: function (response) {
                    var result = response.result;
                    if (result == "SUCCESS") {

                        layer.msg("更新成功");
                        // 重新加载页面
                        generateTree();
                        return;
                    }

                    if (result == "FAILED") {
                        layer.msg("更新失败" + response.message);
                        return;
                    }

                },
                error: function (response) {
                    layer.msg("错误代码：" + response.status + ",错误信息：" + response.statusText);
                }
            })
            $("#menuEditModal").modal("hide");
    })

        $("#treeDemo").on("click",".removeBtn",function (){
            window.id = this.id;
            var zTreeObj = $.fn.zTree.getZTreeObj("treeDemo");
            var currentNode = zTreeObj.getNodeByParam("id",window.id);
            var name = currentNode.name;
            $("#removeNodeSpan").html("&nbsp;&nbsp;<i class='"+currentNode.icon+"'><i/>"+name+"&nbsp;");
            $("#menuConfirmModal").modal("show");
            return false;
        })

        $("#confirmBtn").click(function (){

            $.ajax({
                url: "menu/remove.json",
                type: "POST",
                dataType: "json",
                data: {
                    "id":window.id,
                },
                success: function (response) {
                    var result = response.result;
                    if (result == "SUCCESS") {
                        layer.msg("删除成功");
                        // 重新加载页面
                        generateTree();
                        return;
                    }
                    if (result == "FAILED") {
                        layer.msg("删除失败" + response.message);
                        return;
                    }

                },
                error: function (response) {
                    layer.msg("错误代码：" + response.status + ",错误信息：" + response.statusText);
                }
            })
            $("#menuConfirmModal").modal("hide");
        })



        // check按钮点击时间
        $("#treeDemo").on("click", ".editAuthBtn", function () {
            window.id = this.id;
            $("#assignAuthMenuModal").modal("show");
            fillAssginAuthMenuTree();
            return false;
        })

        $("#assignAuthMenuBtn").click(function () {
            var zTree = $.fn.zTree.getZTreeObj("authTreeDemo");
            var checkedNodes = zTree.getCheckedNodes();
            // 不包含根节点的authId
            var noRootAuthIdArray = [];

            for (let i = 0; i < checkedNodes.length; i++) {
                var currentChecked = checkedNodes[i];
                if(currentChecked.name != null && currentChecked.name != ""){
                    noRootAuthIdArray.push(currentChecked.id);
                }
            }

            var responseBody = {
                "menuId":[window.id],
                "AuthIdArray":noRootAuthIdArray
            }

            responseBody = JSON.stringify(responseBody);

            $.ajax({
                url:"assign/do/menu/assign/auth.json",
                type:"post",
                data:responseBody,
                contentType: "application/json;charset=UTF-8",
                dataType:"json",
                success: function (response) {
                    var result = response.result;
                    if (result == "SUCCESS") {

                        layer.msg("保存成功");
                        // 重新加载页面
                        generateMenuTree();
                        return;
                    }

                    if (result == "FAILED") {
                        layer.msg("保存失败" + response.message);
                        return;
                    }

                },
                error: function (response) {
                    layer.msg("错误代码：" + response.status + ",错误信息：" + response.statusText);
                }
            })

            $('#assignAuthMenuModal').modal('hide');
        })


    })
</script>

<body>
<%@include file="/WEB-INF/include-nav.jsp" %>
<div class="container-fluid">
    <div class="row">
        <%@include file="/WEB-INF/include-sidebar.jsp" %>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">

            <div class="panel panel-default">
                <div class="panel-heading"><i class="glyphicon glyphicon-th-list"></i> 权限菜单列表
                    <div style="float:right;cursor:pointer;" data-toggle="modal" data-target="#myModal"><i
                            class="glyphicon glyphicon-question-sign"></i></div>
                </div>
                <div class="panel-body">
                    <ul id="treeDemo" class="ztree"></ul>
                </div>
            </div>
        </div>
    </div>
</div>
<%@include file="/WEB-INF/modal-menu-add.jsp" %>
<%@include file="/WEB-INF/modal-menu-confirm.jsp" %>
<%@include file="/WEB-INF/modal-menu-edit.jsp" %>
<%@include file="/WEB-INF/modal-menu-assign-auth.jsp" %>
</body>
</html>

